package crmdna.common;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.util.Calendar;

import org.junit.Test;

import crmdna.common.APIResponse.Status;

public class DateUtilsTest {
	@Test
	public void geDateDiffTest() {
		assertEquals("5 seconds", DateUtils.getDateDiff(0, 5));
		assertEquals("1 minute(s)", DateUtils.getDateDiff(0, 65));
		assertEquals("2 minute(s)", DateUtils.getDateDiff(0, 110));
		assertEquals("30 minute(s)", DateUtils.getDateDiff(0, 1820));
		assertEquals("1 hour(s)", DateUtils.getDateDiff(0, 3800));
		assertEquals("3 hour(s)", DateUtils.getDateDiff(0, 11000));

		assertEquals("1 day(s)", DateUtils.getDateDiff(0, 3600 * 24));
		assertEquals("2 day(s)", DateUtils.getDateDiff(0, 3600 * 24 * 2 + 1200));
	}
	
	@Test
	public void getDurationAsString() {
		int start;
		int end;

		// same start and end date
		start = 20130201;
		end = 20130201;
		assertEquals("1 Feb 13", DateUtils.getDurationAsString(start, end));

		// same month and year
		start = 20130201;
		end = 20130204;
		assertEquals("1 - 4 Feb 13", DateUtils.getDurationAsString(start, end));

		// different month same year
		start = 20130227;
		end = 20130304;
		assertEquals("27 Feb - 4 Mar 13", DateUtils.getDurationAsString(start, end));

		// different year
		start = 20131227;
		end = 20140104;
		assertEquals("27 Dec - 4 Jan 14", DateUtils.getDurationAsString(start, end));

		start = 2013123;
		end = 2013125;
		try {
			DateUtils.getDurationAsString(start, end);
			assertTrue(false);
		} catch (APIException ex) {
			assertEquals(Status.ERROR_RESOURCE_INCORRECT,
					ex.statusCode);
		}
	}
	
	@Test
	public void toYYYYMMDDTest() {
		Calendar calendar = Calendar.getInstance();

		// 1-jan-2014
		calendar.set(2014, 0, 1); // month goes from 0 to 11
		assertEquals(20140101, DateUtils.toYYYYMMDD(calendar.getTime()));

		// 31-dec-1989
		calendar.set(1989, 11, 31); // month goes from 0 to 11
		assertEquals(19891231, DateUtils.toYYYYMMDD(calendar.getTime()));

		// 29-feb-2004
		calendar.set(2004, 1, 29); // month goes from 0 to 11
		assertEquals(20040229, DateUtils.toYYYYMMDD(calendar.getTime()));
	}
	
	@Test
	public void isFormatInYYYYMMDD() {
		assertEquals(false, DateUtils.isFormatInYYYYMMDD(2));
		assertEquals(false, DateUtils.isFormatInYYYYMMDD(290394938));
		assertEquals(false, DateUtils.isFormatInYYYYMMDD(2013312));
		assertEquals(false, DateUtils.isFormatInYYYYMMDD(20130229));
		assertEquals(false, DateUtils.isFormatInYYYYMMDD(19000229));
		assertEquals(false, DateUtils.isFormatInYYYYMMDD(21000229));

		assertEquals(true, DateUtils.isFormatInYYYYMMDD(20140201));
		assertEquals(true, DateUtils.isFormatInYYYYMMDD(20140131));
		assertEquals(true, DateUtils.isFormatInYYYYMMDD(20131231));
		assertEquals(true, DateUtils.isFormatInYYYYMMDD(20140630));
	}
}
